package com.smsc.headend.connector.controller;


import com.smsc.headend.connector.service.DeviceStatusService;
import com.smsc.headend.connector.service.FtpServerService;
import com.smsc.headend.connector.service.LogTerminalAccessService;
import com.smsc.headend.connector.service.TerminalChannelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


/**
 * @author liangli
 * @date 2020/10/22
 */
@Component
@Slf4j
public class ScheduleController {
    @Autowired
    FtpServerService ftpServerService;
    @Autowired
    LogTerminalAccessService logTerminalAccessService;

    @Autowired
    TerminalChannelService terminalChannelService;
    @Autowired
    DeviceStatusService deviceStatusService;

    @Value("${collection.ftp.enable}")
    Boolean ftpCollectEnable;
    private ScheduledExecutorService ftpCollectExecutorService = Executors.newSingleThreadScheduledExecutor();
    private ScheduledExecutorService ftpServerCheckService = Executors.newSingleThreadScheduledExecutor();
    private ScheduledExecutorService comEntityOffLineCheckExecutorService = Executors.newSingleThreadScheduledExecutor();
    private ScheduledExecutorService trafficPersistenceExecutorService = Executors.newSingleThreadScheduledExecutor();
    private ScheduledExecutorService plcMeterStatusCheckService = Executors.newSingleThreadScheduledExecutor();

    // default 1440 minutes (one day)
    @Value("${device.plcMeterStatus.scan.interval:1440}")
    Integer plcMeterStatusScanInterval;

    @PostConstruct
    public void init() {
        log.info("init ftp collection, status {} ", ftpCollectEnable);
        if (ftpCollectEnable) {
            this.runFtpCollection();
        }
    }

    public void runFtpCollection() {
        //ftp定抄
        ftpCollectExecutorService.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                try {
                    ftpServerService.collectRemoteFiles();
                } catch (Exception e) {
                    log.error("collect remote file fail", e);
                }
            }
        }, 0, 30, TimeUnit.SECONDS);
        //刷新FTP server
        ftpServerCheckService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                try {
                    ftpServerService.refreshCollectionServer();
                } catch (Exception e) {
                    log.error("refresh collection server fail", e);
                }
            }
        }, 0, 5 * 60, TimeUnit.SECONDS);

        comEntityOffLineCheckExecutorService.scheduleAtFixedRate(() -> {
            try {
                terminalChannelService.checkOfflineComEntity();
            } catch (Exception throwable) {
                log.error("check offline ", throwable);
            }
        }, 20, 5 * 60, TimeUnit.SECONDS);

//        plcMeterStatusCheckService.scheduleAtFixedRate(() -> {
//            try {
//                deviceStatusService.checkPlcMetersStatus();
//            } catch (Exception throwable) {
//                log.error("check plcMeterStatus offline ", throwable);
//            }
//        }, 20, plcMeterStatusScanInterval, TimeUnit.MINUTES);
    }
}
